/*
 * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef AT_USER_DEBUG

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lega_at_api.h"
#ifdef CFG_PLF_RV32
#include "asr_common.h"
#include "asr_flash.h"
#include "alto_flash_kv.h"
#else
#include "lega_common.h"
#endif
#include "flash.h"
#include "asr_rv32.h"
//#define CFG_HARMONY_SUPPORT
/*
 ************************************************************
 *                    USER AT CMD START
 *
 ************************************************************
 */

#ifdef CFG_HARMONY_SUPPORT
void openharmony_init()
{
    extern void  OHOS_SystemInit(void);
    OHOS_SystemInit();

}
#endif
int at_test_1(int argc, char **argv)
{
    int tempValue=1;
    switch(argc)
    {
        case 1:
            printf("%s...%s\n",__func__, argv[0]);
            break;
        case 2:
        {
            uint32_t * tmp=NULL;
            printf("%s...%s\n", __func__, argv[1]);
            if(strcmp(argv[1],"coredump")==0){
                tmp = 0xDEADBEEF;
                *tmp = tempValue;
            }
            break;
        }
        case 3:
            tempValue = convert_str_to_int(argv[2]);
            if(tempValue == LEGA_STR_TO_INT_ERR)
            {
                printf("error param\n");
                break;
            }
            printf("%s...%s...%d\n", __func__, argv[1], tempValue);
            break;
        default:
            printf("error param\n");
            break;
    }

    return CONFIG_OK;
}
int at_harmony_xts(int argc, char **argv)
{
#ifdef CFG_HARMONY_SUPPORT
    openharmony_init();
#endif
    return CONFIG_OK;
}

//int at_hilink_start(int argc, char **argv)
//{
//    hilink_main_wrap();
//    return CONFIG_OK;
//}

int at_ota_init_test(int argc, char **argv)
{
    extern void lalala_ota_init(void);
    lalala_ota_init();
    return CONFIG_OK;
}
int at_kv_clear(int argc, char **argv)
{
    asr_flash_erase(PARTITION_PARAMETER_2, 0, KV_MAX_SIZE);
    printf("kv clear done\r\n");
    return CONFIG_OK;
}

int at_test_kv(int argc, char **argv)
{
    int i=0,ret=0;
    printf("%s...\n",__func__);
    for(i=0;i<argc;i++)
        printf("%s ",argv[i]);
    printf("\n");
    #if 0
    if(strcmp(argv[1],"set")==0)
    {
        lega_rtos_declare_critical();
        lega_rtos_enter_critical();
        ret = duet_flash_kv_set(argv[2],argv[3],strlen(argv[3])+1,1);
        lega_rtos_exit_critical();
        if(ret==0)
            printf("success!\n");
        else
            printf("fail!\n");
    }
    else if(strcmp(argv[1],"get")==0)
    {
        char kvBuffer[128]={0};
        int32_t len=128;
        lega_rtos_declare_critical();
        lega_rtos_enter_critical();
        ret = duet_flash_kv_get(argv[2],kvBuffer,&len);
        lega_rtos_exit_critical();
        if(ret==0)
            printf("%s = %s\n",argv[2],kvBuffer);
        else
            printf("fail!\n");
    }
    else if(strcmp(argv[1],"runMore")==0)
    {
        int i=0,count=atoi(argv[4]);
        char key[128]={0},value[128]={0};
        //memcpy(buffer,argv[2],strlen(argv[2]));
        lega_rtos_declare_critical();
        lega_rtos_enter_critical();
        for(i=0;i<count;i++){
            sprintf(key,"%s_%d",argv[2],i);
            sprintf(value,"%s_%d",argv[3],i);
            printf("set %s %s\n",key,value);
            ret = duet_flash_kv_set(key,value,strlen(value)+1,1);
            lega_rtos_exit_critical();
            if(ret==0)
                printf("success!\n");
            else
                printf("fail!\n");
        }

    }
    else if(strcmp(argv[1],"flashset")==0)
    {
        uint32_t offset = 0x00;
        __disable_irq();
        ret = duet_flash_erase_write(PARTITION_PARAMETER_2,&offset,argv[2],strlen(argv[2]+1));
         __enable_irq();
        if(ret==0)
            printf("success!\n");
        else
            printf("fail!\n");
    }
    else if(strcmp(argv[1],"flashget")==0)
    {
        char buffer[128]={0};
        uint32_t offset = 0x00;
        __disable_irq();
        ret = duet_flash_read(PARTITION_PARAMETER_2,&offset,buffer,128);
         __enable_irq();
        if(ret==0)
            printf("read %s\n",buffer);
        else
            printf("fail!\n");
    }
    else
        printf("Invalid input!\n");
#endif
    return CONFIG_OK;
}

static void hex_to_str(char* ihex, char* istr, uint8_t n)
{
    uint8_t i,j;
    for(i = 0,j = 0; i < n; i++)
    {
        j = i << 1;
        istr[j+1] = ihex[i] & 0x0f;
        istr[j+1] += (istr[j+1] < 10 ? '0' : 'A' - 10);
        istr[j] = (ihex[i] >> 4) & 0x0f;
        istr[j] += (istr[j] < 10 ? '0' : 'A' - 10);
        //cli_printf("the passphrase[%d] is %c\r\n",j,_str[j]);
    }
}

static int at_setlog_cmd(int32_t argc, char **argv)
{
    if(argc > 1)
    {
        char mac[6] = {0};
        char passphrase[25] = {0};
        lega_wlan_get_mac_address(mac);

        hex_to_str(mac,passphrase,6);

        //½«ÊäÈëmacÖÐº¬ÓÐµÄÐ¡Ð´×ÖÄ¸×ª»¯Îª´óÐ´
        for(uint8_t i = 0; i < 12; i++)
        {
            if(argv[1][i] >= 'a' && argv[1][i] <= 'z')
            {
                argv[1][i] -= 32;
            }
        }

        if(strncmp(argv[1], "0", sizeof("0")) == 0)
        {
            printf("log_disable\r\n");
            lega_log_disable();
        }
        else if(strncmp(argv[1], passphrase, sizeof(passphrase)) == 0)
        {
            lega_log_enable();
            printf("log_enable\r\n");
        }
    }
    return 0;
}

lega_at_cmd_entry at_cmd_table[] =
{
    {"at_test_1",at_test_1},
    {"at_harmony_xts",at_harmony_xts},
    {"testkv",at_test_kv},
};

/*
 ************************************************************
 *                    USER AT CMD END
 *
 ************************************************************
 */

cmd_entry comm_test1 = {
    .name = "at_test1",
    .help = "at_test1",
    .function = at_test_1,
};
cmd_entry comm_harmony_xts = {
    .name = "at_harmony_xts",
    .help = "at_harmony_xts",
    .function = at_harmony_xts,
};
//cmd_entry comm_hilink_start = {
//    .name = "at_hilink_start",
//    .help = "at_hilink_start",
//    .function = at_hilink_start,
//};
cmd_entry comm_ota_init_test = {
    .name = "at_ota_init",
    .help = "at_ota_init",
    .function = at_ota_init_test,
};
cmd_entry comm_at_kv_clear = {
    .name = "at_kv_clear",
    .help = "at_kv_clear",
    .function = at_kv_clear,
};
cmd_entry comm_testkv = {
    .name = "testkv",
    .help = "testkv",
    .function = at_test_kv,
};
cmd_entry comm_at_log = {
    .name = "AT+LOG",
    .help = "log switch",
    .function = at_setlog_cmd,
};

void lega_at_user_cmd_register(void)
{
    lega_at_cmd_register(&comm_test1);
    lega_at_cmd_register(&comm_harmony_xts);
    //lega_at_cmd_register(&comm_hilink_start);
    lega_at_cmd_register(&comm_ota_init_test);
    lega_at_cmd_register(&comm_at_kv_clear);
    lega_at_cmd_register(&comm_testkv);
    lega_at_cmd_register(&comm_at_log);
}
#endif
